<?php
namespace app\admin\controller;

use think\Loader;
use app\admin\model\Base;

class Chip extends Common {

    const STOCK_PRICE = 'http://qt.gtimg.cn/q=';

    public function _initialize(){

        $this->securitie = new \app\admin\model\Securities();
        $this->baseInfo = new \app\admin\model\Baseinfo();
        $this->chip = new \app\admin\model\Chip();
    }

    public function index() {

        $list = $this->securitie->securitieList(['status'=>9]);
        foreach($list AS $k=>$v){
            if($v['sid']==null){
                unset($list[$k]);
            }
        }
        $this->assign('lists', $list);
        return $this->fetch();
     }

    public function indexOld() {
        $where = [];
        $name = input('name');

        if($name){
            $where['se.name'] = ['like',"%{$name}%"];
        }
        $where['se.status'] = 9;

        $list = $this->chip->alias('c')
                     ->join('securities se', 'c.sid=se.sid', 'LEFT')
                     ->where($where)
                     ->field('c.*,se.sid,se.stock_code,se.stock_name,se.jys')
                     ->order('c.cid desc')
                     ->select();

        if($list != null){

            foreach($list AS &$v){
                $v->jys = mb_strtolower($v->jys);
                $tmpPrice = file_get_contents(self::STOCK_PRICE.$v->jys.$v->stock_code);
                $current_encode = mb_detect_encoding($tmpPrice, array("ASCII","GB2312","GBK",'BIG5','UTF-8'));
                $v->price = mb_convert_encoding($tmpPrice, 'UTF-8', $current_encode);
                $v->price = explode('~',$v->price)[3];
                $v->poor = sprintf("%.2f",substr(sprintf("%.4f", $v->seventy_percent/$v->price), 0, -2));
                $v->poor_bj = '';
                $v->score = '';
                if($v->poor>=1){
                    $v->poor_bj = '#FF5C5C;';
                    $v->score = intval($v->score)+1;
                }
            }
            $base = new Base();
            $list = $base->pegSort($list, $sort='SORT_DESC', 'poor', 'poor_bj');
        }

        $this->assign('lists', $list);
        $this->assign('name', $name);
        return $this->fetch();
    }

    //编辑页
    public function editp(){

        $data = input();
        if ($data['sid'] && $data['stock_code']) {

            //当前用户信息
            $baseInfo = $this->baseInfo->detailStockBaseInfo($data['sid'], $data['stock_code']);
            $detail = $this->securitie->detailCompanyInfo($data['sid'], $data['stock_code']);
            $baseInfo['stock_name'] = $detail['companyInfo']['stock_name'];
            $baseInfo['institutions_name'] = $detail['companyInfo']['institutions_name'];

            $chip = null;
            $chip = $this->chip->chipDetail(['sid'=>$data['sid']]);
            $baseInfo['average_cost'] = $chip['average_cost'];
            $baseInfo['seventy_percent'] = $chip['seventy_percent'];
            $baseInfo['ninety_percent'] = $chip['ninety_percent'];
            $baseInfo['market_concentration'] = $chip['market_concentration'];
            $this->assign('detail', $baseInfo);
        }
        return $this->fetch();
    }

    //编辑页
    public function edit(){

        $data = input();
        $this->baseInfo->editStockBaseInfo($data);
        $this->success('操作成功', url('index'));
    }

    //删除
    public function del() {

        $data = input();
        $this->securitie->securitieInfoUpdate($data['sid'], $data['stock_code'],['status'=>$data['status']]);
        $this->success('操作成功', url('index'));
    }

    //状态修改
    public function statusUpdate() {
        $sid = input('sid');
        $stock_code = input('stock_code');
        $this->securitie->securitieInfoUpdate($sid, $stock_code);
        $this->success('操作成功', url('index'));
    }
}